/**
 * 
 */
package fr.ants.view.panel;

import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.TransferHandler;

import fr.ants.controller.listener.ButtonsListener;
import fr.ants.view.Items;
import fr.ants.view.PictureManage;
import fr.ants.view.Selection;

/**
 * This panel contain all tools buttons.
 * 
 * @author Guillaume Ruscassie
 */
public class ButtonsToolsPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3483663359410180519L;
	/**
	 * button resource.
	 */
	private final JButton resourceButton;
	/**
	 * button to add a obstacle.
	 */
	private final JButton obstacleButton;
	/**
	 * selection button
	 */
	private final JButton selectionButton;

	/**
	 * @param transferHandler
	 *            for the drag and drop.
	 * @param listener
	 *            for the buttons
	 */
	public ButtonsToolsPanel(final ButtonsListener buttonsListener,
			final TransferHandler transferHandler) {
		// add the resource button
		resourceButton = new JButton(
				PictureManage.createIconToolBar(Items.RESOURCE.getIcon()));
		resourceButton.setToolTipText(Items.RESOURCE.getLabel());
		resourceButton.setTransferHandler(transferHandler);
		resourceButton.addMouseListener(buttonsListener);
		resourceButton.addMouseMotionListener(buttonsListener);
		resourceButton.setActionCommand(Items.RESOURCE.getActionCommand());
		resourceButton.setEnabled(false);
		resourceButton.setBackground(Color.LIGHT_GRAY);
		add(resourceButton);

		// add the button obstacle
		obstacleButton = new JButton(
				PictureManage.createIconToolBar(Items.OBSTACLE.getIcon()));
		obstacleButton.setTransferHandler(transferHandler);
		obstacleButton.addMouseListener(buttonsListener);
		obstacleButton.addMouseMotionListener(buttonsListener);
		obstacleButton.setToolTipText(Items.OBSTACLE.getLabel());
		obstacleButton.setActionCommand(Items.OBSTACLE.getActionCommand());
		obstacleButton.setEnabled(false);
		obstacleButton.setBackground(Color.LIGHT_GRAY);
		add(obstacleButton);

		// add the button selection
		selectionButton = new JButton(
				PictureManage.createIconToolBar(Items.SELECTION.getIcon()));
		selectionButton.setToolTipText(Items.SELECTION.getLabel());
		selectionButton.setActionCommand(Items.SELECTION.getActionCommand());
		selectionButton.addMouseListener(buttonsListener);
		selectionButton.setEnabled(false);
		selectionButton.setBackground(Color.GREEN);
		add(selectionButton);
	}

	/**
	 * to enable or not all buttons of the panel.
	 * 
	 * @param b
	 *            true to enable otherwise false.
	 */
	public void enableButtons(final boolean b) {
		resourceButton.setEnabled(b);
		obstacleButton.setEnabled(b);
		selectionButton.setEnabled(b);
	}
	
	/**
	 * to see what mode is active
	 * 
	 * @param Selection sel
	 */
	
	public void activeButtons(Selection sel) {
		if(sel.equals(Selection.RESOURCE)) {
			resourceButton.setBackground(Color.GREEN);
			obstacleButton.setBackground(Color.LIGHT_GRAY);
			selectionButton.setBackground(Color.LIGHT_GRAY);
		}
		
		else if(sel.equals(Selection.OBSTACLE)) {
			resourceButton.setBackground(Color.LIGHT_GRAY);
			obstacleButton.setBackground(Color.GREEN);
			selectionButton.setBackground(Color.LIGHT_GRAY);
		}
		
		else if(sel.equals(Selection.SELECTION) || sel.equals(Selection.CLICK_RIGHT)) {
			resourceButton.setBackground(Color.LIGHT_GRAY);
			obstacleButton.setBackground(Color.LIGHT_GRAY);
			selectionButton.setBackground(Color.GREEN);
		}
		else {
			resourceButton.setBackground(Color.LIGHT_GRAY);
			obstacleButton.setBackground(Color.LIGHT_GRAY);
			selectionButton.setBackground(Color.LIGHT_GRAY);
		}
	}
}